<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件转换器</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }

        h1 {
            color: #333;
            text-align: center;
        }

        form {
            background: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            max-width: 500px;
            margin: 0 auto;
        }

        label {
            display: block;
            margin-bottom: 10px;
            font-weight: bold;
        }

        input[type="file"],
        select,
        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #5cb85c;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background-color: #4cae4c;
        }

        h2 {
            margin-top: 30px;
            text-align: center;
        }

        pre {
            background: #fff;
            padding: 10px;
            border-radius: 5px;
            overflow: auto;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <h1>文件转换器</h1>
    <form id="upload-form">
        <label for="files">选择文件：</label>
        <input type="file" name="files" id="files" multiple required><br>

        <label for="format">选择目标格式：</label>
        <select name="format" id="format" required>
            <option value="pdf">PDF</option>
            <option value="png">PNG</option>
            <option value="docx">DOCX</option>
            <option value="md">Markdown</option>
        </select>

        <label for="encoding">编码（用于 CSV/TSV）：</label>
        <input type="text" name="encoding" id="encoding" value="utf-8"><br>

        <button type="submit"><i class="fas fa-file-upload"></i> 转换</button>
    </form>

    <h2>结果</h2>
    <pre id="results"></pre>

    <script>
        $(document).ready(function() {
            $('#upload-form').on('submit', function(e) {
                e.preventDefault();
                var formData = new FormData(this);
                $.ajax({
                    url: '/convert',
                    type: 'POST',
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function(data) {
                        $('#results').empty();
                        data.forEach(function(result) {
                            if (result.status === 'done') {
                                $('#results').append(`文件: ${result.file} - 转换为: ${result.new_path || result.new_paths.join(', ')}\n`);
                            } else {
                                $('#results').append(`文件: ${result.file} - 错误: ${result.error}\n`);
                            }
                        });
                    },
                    error: function() {
                        $('#results').append('处理文件时发生错误。');
                    }
                });
            });
        });
    </script>
</body>
</html>

